/*
 * Facebox (for jQuery)
 * version: 1.2 (05/05/2008)
 * @requires jQuery v1.2 or later
 *
 * Examples at http://famspam.com/facebox/
 *
 * Licensed under the MIT:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Copyright 2007, 2008 Chris Wanstrath [ chris@ozmm.org ]
 */
(function($){
$.facebox = function(s) {
	if (typeof s == "string") s = { dom: s };
	$.extend($.facebox.s, $.facebox.def, s);
	$.facebox.loading();
	$.facebox.reveal();
};
$.fn.facebox = function(s) {
	s.dom = $(this).html();
	$.facebox(s);
};

$.extend($.facebox, {
	s: {},
	def: {
		klass: "",
		dom: "",
		url: "",
		fx: {
			overlay: true,
			opacity: 0.8
		}
	},
	html: '<div id="facebox" style="display:none;">'
		+ '<div class="popup">'
		+ '<table>'
		+ '<tbody>'
		+ '<tr>'
		+ '<td class="tl"/><td class="b"/><td class="tr"/>'
		+ '</tr>'
		+ '<tr>'
		+ '<td class="b"/>'
		+ '<td class="body">'
		+ '<div class="content"></div>'
		+ '<div class="footer">'
		+ '<a href="#" class="close">'
		+ '<img src="facebox/closelabel.gif" title="close" class="close_image" />'
		+ '</a>'
		+ '</div>'
		+ '</td>'
		+ '<td class="b"/>'
		+ '</tr>'
		+ '<tr>'
		+ '<td class="bl"/><td class="b"/><td class="br"/>'
		+ '</tr>'
		+ '</tbody>'
		+ '</table>'
		+ '</div>'
		+ '</div>',

	loading: function() {
		init();
		if ($("#facebox .loading").length == 1) return true;
		overlay();
	
		$("#facebox .content").empty();
		$("#facebox .body").children().hide().end().append("<div class='loading'><img src='facebox/loading.gif'/></div>");
	
		$('#facebox').css({
			top:  getPageScroll()[1] + ($(window).height() / 10),
			left: $(window).width() / 2 - 205 
		}).show();
	
		$(document).bind("keydown.facebox", function(e) {
			if (e.keyCode == 27) $.facebox.close();
			return true;
		});
		$(document).trigger("loading.facebox");
	},
	
	reveal: function() {
		$(document).trigger("beforeReveal.facebox");
		if ($.facebox.s.klass) $("#facebox .content").addClass($.facebox.s.klass);
		fill();
		$("#facebox .loading").remove();
		$("#facebox .body").children().fadeIn("normal");
		$("#facebox").css("left", $(window).width() / 2 - ($("#facebox table").width() / 2));
		$(document).trigger("reveal.facebox").trigger("afterReveal.facebox");
	},
	
	close: function() {
		$(document).trigger("close.facebox");
		return false;
	}
});

/*
* Private methods
*/

function init() {
	if ($.facebox.s.inited) {
		return true;
	} else {
		$.facebox.s.inited = true;
	}

	$(document).trigger("init.facebox");

	$("body").append($.facebox.html);

	$("#facebox .close").click($.facebox.close);
	$("#facebox .close_image").attr("src", "facebox/closelabel.gif");
}

function fill() {
	var box = $("#facebox .content");
	var html = $.facebox.s.dom;
	var sel = $(document).find(html);
	var url = $.facebox.s.url;
	// We assume it's a selector, but if it's not (or there is no matching element), assume it's HTML.
	if (sel && sel.length > 0) {
		box.html(sel.html());
	} else if (url && url.length > 0) {
		if (/$\.jpg|jpeg|png|gif|bmp/.test(url)) {
			box.html("<img src='" + url + "'>");
		} else {
			box.load(url);
		}
	} else {
		box.html(html);
	}
}

// getPageScroll() by quirksmode.com
function getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	return [ xScroll, yScroll ];
}

function overlay() {
	if ($.facebox.s.fx.overlay === true) {
		if ($("#facebox_overlay").length === 0) {
			$("body").append("<div id='facebox_overlay' class='facebox_hide'></div>");
		}
		$("#facebox_overlay").hide().addClass("facebox_overlayBG").css("opacity", $.facebox.s.fx.opacity).click(function(){ $(document).trigger("close.facebox"); });
		$("#facebox, #facebox_overlay").fadeIn("def");
	} else if ($.facebox.s.fx.overlay === false) {
		$.facebox.s.fx.overlay === false || $.facebox.s.fx.opacity === null;
	}
}

/*
* Bindings
*/
$(document).bind("close.facebox", function() {
	$(document).unbind("keydown.facebox");
	$("#facebox").fadeOut(function() {
		if ($("#facebox_moved").length === 0) {
			$("#facebox .content").removeClass().addClass("content");
		} else {
			$("#facebox_moved").replaceWith($("#facebox .content").children().hide());
		}
		if ($("#facebox_overlay").length == 1) {
			$("#facebox_overlay").fadeOut("def", function(){
				$("#facebox_overlay").removeClass("facebox_overlayBG");
				$("#facebox_overlay").addClass("facebox_hide");
				$("#facebox_overlay").remove();
			});
		}
		$("#facebox .loading").remove();
	});
});
})(jQuery);

/*
Basic utilities
*/
function id(id) { return document.getElementById(id); }

function shortCut(toDo, extra) {
	if (toDo == "editor") {
		$("#optionsDiv, #boxDiv").hide();
		$("#"+extra).show();
	} else if (toDo == "new") {
		$.facebox({ dom: jbzw.d.New, fx: { fx: extra } });
	} else if (toDo == "open") {
		$.facebox({ dom: jbzw.d.Open, fx: { overlay: extra } });
	} else if (toDo == "saveDisk") {
		var map = id("bzwCode").value;
		$.ajax({
			type: "POST",
			url: "php.php",
			data: "mode=savedisk&map="+encodeURI(map),
			success: function(it){
				eval(it);
			}
		});
	} else if (toDo == "popMap") {
		id("bzwCode").value = $("#hiddenFrame > div").html();
	} else {
		$.facebox({ dom: jbzw.d[toDo], fx: { overlay: extra } });
	}
}

function num(n) {
	n = String(n).replace(/\s/g, "");
	if (isNaN(n)) n = 0;
}

$.fn.use = function(cb){
	this.cb;
	return this;
};

$.fn.xulcss = function(style){
	var elem = this.get(0);
	var obj = elem.ownerDocument.defaultView.getComputedStyle(elem, null);
	return obj.getPropertyValue(style);
};